package org.jeecg.modules.medical.areaManage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.medical.areaManage.entity.Area;
import org.jeecg.modules.medical.areaManage.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 区域信息表 前端控制器
 * </p>
 *
 * @author yaorange
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/area")
public class AreaController {
        @Autowired
        private IAreaService iAreaService;
    /**
     * 查询所有根节点
     *
     * @return
     */
    @GetMapping(value = "/listRoot")
    public Result<?> listRoot() {
        // 查询所有根节点
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id","");
        queryWrapper.orderByAsc("sort");
        List<Area> areaList = iAreaService.list(queryWrapper);
        for (Area area : areaList) {
            // 根节点默认有子节点
            area.setHasChildren(true);
        }
        return Result.OK(areaList);
    }

    /**
     * 根据父节点Id 查询所有子节点
     *
     * @return
     */
    @GetMapping(value = "/listChildren")
    public Result<?> listChildren(@RequestParam String parentId) {
        // 查询所有根节点,
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.orderByAsc("sort");
        List<Area> areaList = iAreaService.list(queryWrapper);
        for (Area area : areaList) {
            // 单位小于2时，默认有子节点，因为unit值为2的乡镇/街道没有子节点
            if (area.getIsLeaf() < 2) {
                //让当前节点显示折叠符号（+）
                area.setHasChildren(true);
            }
        }
        return Result.OK(areaList);
    }
    @GetMapping("/list")
    public Result<?> getLIst(@RequestParam String name){
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        queryWrapper.orderByAsc("sort");
        List<Area> areaList = iAreaService.list(queryWrapper);
        return Result.OK(areaList);
    }
}

